package com.car.serve.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.commons.core.model.Result;
import com.car.serve.Param.PageServeShouHouParam;
import com.car.serve.Param.PageStoresShouHouLogParam;
import com.car.serve.Param.PageStoresShouHouParam;
import com.car.serve.client.UserClient;
import com.car.serve.entity.ServeShouhou;
import com.car.serve.entity.ServeShouhouLog;
import com.car.serve.mapper.ServeShouhouLogMapper;
import com.car.serve.service.ServeShouhouLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 董旭
 * @since 2024-08-28
 */
@Service
public class ServeShouhouLogServiceImpl extends ServiceImpl<ServeShouhouLogMapper, ServeShouhouLog> implements ServeShouhouLogService {

    @Resource
    private ServeShouhouLogMapper serveShouhouLogMapper;
    @Resource
    private UserClient userClient;
    @Override
    public Page<ServeShouhouLog> findBuyIdShouHouLogPage(PageServeShouHouParam param) {
        Page<ServeShouhouLog> page = new Page<>(param.getPageNum() == null ? 1 : param.getPageNum(), param.getPageSize() == null ? 6 : param.getPageSize());
        QueryWrapper<ServeShouhouLog> wrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(param.getServeState())){
            wrapper.like("serve_state",param.getServeState());
        }
        wrapper.orderByDesc("serve_log_completiontime");
        Page<ServeShouhouLog> serveShouhouLogPage = serveShouhouLogMapper.selectPage(page, wrapper);
        return serveShouhouLogPage;
    }

    @Override
    public Page<ServeShouhouLog> getShouhouLogPage(PageStoresShouHouLogParam param) {
        //根据当前登录ID查询门店信息
        Result<Integer> result = userClient.findStoresIdByClerkId(param.getClerkId());
        Integer storesId = result.getData();
        Page<ServeShouhouLog> page = new Page<>(param.getPageNum() == null ? 1 : param.getPageNum(), param.getPageSize() == null ? 6 : param.getPageSize());
        QueryWrapper<ServeShouhouLog> wrapper = new QueryWrapper<>();
        // 添加当前的门店ID作为查询条件
        wrapper.eq("stores_id", storesId).eq("serve_log_state",2);
        // 添加排序规则，比如按照创建时间降序排序
        wrapper.orderByDesc("serve_log_completiontime");
        Page<ServeShouhouLog> serveShouhouLogPage = serveShouhouLogMapper.selectPage(page, wrapper);
        return serveShouhouLogPage;
    }
}
